package com.nuc.iblog.service.imp;

import com.nuc.iblog.entity.Music;
import com.nuc.iblog.entity.Picture;
import com.nuc.iblog.entity.User;
import com.nuc.iblog.entity.Video;
import com.nuc.iblog.jpa.MusicJpa;
import com.nuc.iblog.jpa.PictureJpa;
import com.nuc.iblog.jpa.VideoJpa;
import com.nuc.iblog.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClMediaServiceimp implements MediaService {
    @Autowired
    MusicJpa musicJpa;
    @Autowired
    VideoJpa videoJpa;
    @Autowired
    PictureJpa pictureJpa;
    @Override
    public List<Music> getAllMusic(User user) {
        return musicJpa.findAllByUser(user);
    }

    @Override
    public List<Picture> getAllPicture(User user) {
        return pictureJpa.findAllByUser(user);
    }

    @Override
    public List<Video> getAllVideo(User user) {
        return videoJpa.findAllByUser(user);
    }

    @Override
    public Music getMusic(int mid) {
        return musicJpa.getOne(mid);
    }

    @Override
    public Picture getPicture(int pid) {
        return pictureJpa.getOne(pid);
    }

    @Override
    public Video getVideo(int vid) {
        return videoJpa.getOne(vid);
    }

    @Override
    public Page<Music> getPageMusic(User user, int page) {
        Pageable pageable=new PageRequest(page,9);
        return musicJpa.findAllByUser(user,pageable);

    }

    @Override
    public Music getbackgroundmusic(int isbackground,User user) {
        return musicJpa.findByIsbackgroundAndUser(isbackground,user);
    }

    @Override
    public Page<Picture> getPagePicture(User user,int page) {
        Pageable pageable=new PageRequest(page,9);
        return pictureJpa.findAllByUserOrderByPidDesc(user,pageable);
    }

    @Override
    public Page<Video> getPageVideo(User user,int page) {
        Pageable pageable=new PageRequest(page,9);
        return videoJpa.findAllByUser(user,pageable);
    }

    @Override
    public int deleteMusic(int mid) {
        musicJpa.delete(mid);
        return 1;
    }

    @Override
    public int deleteVideo(int vid) {
        videoJpa.delete(vid);
        return 1;
    }

    @Override
    public int deletePicture(int pid) {
        pictureJpa.delete(pid);
        return 1;
    }
}
